package org.w3.schools.ws;

import static org.mockito.Mockito.when;

import javax.annotation.Resource;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.ExpectedException;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.w3.schools.ws.v1_0.TempConvertSoap;

import com.exception.ws.BusinessException;
import com.exception.ws.TechnicalException;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:tempconvert-test-context.xml" })
public class TempConvertConnectortest {

	private static Logger log = LoggerFactory.getLogger(TempConvertConnectortest.class);

	@Autowired
	private TempConvertConnector tempConvertConnector;

	@Resource
	private TempConvertSoap wsTempConvertClientPortType;

	@Before
	public void setup() {
		//wsTempConvertClientPortType = Mockito.mock(TempConvertSoap.class);
	}

	@Test
	public void should_convert_celsius_to_fahrenheit() {
		log.info("=================================================================================================================================");
		log.info("===============================should_convert_celsius_to_fahrenheit ====================================");
		String value = "0";
		String response = "32";

		when(wsTempConvertClientPortType.celsiusToFahrenheit(value)).thenReturn(response);

		try {
			String retour = tempConvertConnector.celsiusToFahrenheit(value);
			Assert.assertEquals("should be equal", "32", retour);
		} catch (TechnicalException e) {
			log.error("ERROR", e);
			Assert.fail();
		} catch (BusinessException e) {
			log.error("ERROR", e);
			Assert.fail();
		}

	}

	@Test
	public void should_convert_fahrenheit_to_celsius() {
		log.info("=================================================================================================================================");
		log.info("===============================should_convert_fahrenheit_to_celsius ====================================");
		String value = "32";
		String response = "0";

		when(wsTempConvertClientPortType.fahrenheitToCelsius(value)).thenReturn(response);

		try {
			String retour = tempConvertConnector.fahrenheitToCelsius(value);
			Assert.assertEquals("should be equal", "0", retour);
		} catch (TechnicalException e) {
			log.error("ERROR", e);
			Assert.fail();
		} catch (BusinessException e) {
			log.error("ERROR", e);
			Assert.fail();
		}

	}
	
	
	@Test
	@ExpectedException(IllegalArgumentException.class)
	public void should_raise_exception_because_in_parameter_is_not_numeric() throws TechnicalException, BusinessException{
		log.info("=================================================================================================================================");
		log.info("===============================should_raise_exception_because_in_parameter_is_not_numeric ====================================");
		
		String value = "pp";
		String response = "Error";

		when(wsTempConvertClientPortType.fahrenheitToCelsius(value)).thenReturn(response);

		@SuppressWarnings("unused")
		String retour = tempConvertConnector.fahrenheitToCelsius(value);
		Assert.fail();
		
	}

}
